<?php
class AteneosController extends AppController {

	var $name = 'Ateneos';
	var $components = array('Email'); //  use component email 
	var $uses = array('User','Client','Ateneo');
    var $helpers = array('Xml');

    function beforeFilter () {
        parent::beforeFilter();

		// Aplico el Layout segun la Vista o si es AJAX
		if ($this->RequestHandler->isAjax()) {
			$this->layout = 'ajax';
		}else{
			// Cambio la Vista por la del Rol
			$this->layout = strtolower($this->Session->read('VistaActual'));
		}
    }

    function index() {

		$client_id = $this->Session->read('ClientesUsuarioLogueado.id'); // $_SESSION['Client']['id'];
		$user_id   = $this->Auth->user('id'); //$_SESSION['Auth']['User']['id'];

		$Ateneos = $this->Ateneo->find('all', array(
						'conditions' => array(
										'Ateneo.client_id' => $client_id,
										'Ateneo.user_id'    => $user_id,
						), 'recursive' => 1,
						'order' => array('Ateneo.id'=>'DESC')
						));

		// Obtengo la cantidad de comentarios recibidos
		$array_cant_respuestas = "";
		foreach ($Ateneos as $key => $registro) {

			$url_api = 'http://www.comunidadaps.org.ar/api/obtenercomentarios.php?idt='.$registro['Ateneo']['id_remoto'];
			$a       = file_get_contents($url_api);
			$r       = utf8_encode($a);
			$result  = json_decode($r,true);
			if($result){
				$comentarios = $result[0]['comentarios'];
				$array_cant_respuestas[$registro['Ateneo']['id']] = count($comentarios);
			}else{
				$array_cant_respuestas[$registro['Ateneo']['id']] = 0;
			}
		}
//pr($Ateneos);
		$this->set('Ateneos',$Ateneos);
		$this->set('array_cant_respuestas',$array_cant_respuestas);
    }


    function ver($ateneo_id=0) {

		$client_id = $this->Session->read('ClientesUsuarioLogueado.id'); // $_SESSION['Client']['id'];
		$user_id   = $this->Auth->user('id'); //$_SESSION['Auth']['User']['id'];

		$Ateneo = $this->Ateneo->find('first', array(
						'conditions' => array(
										'Ateneo.client_id' => $client_id,
										'Ateneo.user_id'   => $user_id,
										'Ateneo.id'        => $ateneo_id,
						), 'recursive' => 1

						));
		$this->set('Ateneo',$Ateneo);

		$url_api = 'http://www.comunidadaps.org.ar/api/obtenercomentarios.php?idt='.$Ateneo['Ateneo']['id_remoto'];
		$a       = file_get_contents($url_api);
		$r       = utf8_encode($a);
		$result  = json_decode($r,true);
		if($result){
			$comentarios = $result[0]['comentarios'];
			$this->set('comentarios',$comentarios);
		}



    }

    function crear($ateneo_id=0) {

		$client_id = $this->Session->read('ClientesUsuarioLogueado.id'); // $_SESSION['Client']['id'];
		$user_id   = $this->Auth->user('id'); //$_SESSION['Auth']['User']['id'];

		$Ateneo = $this->Ateneo->find('first', array(
						'conditions' => array(
										'Ateneo.client_id' => $client_id,
										'Ateneo.user_id'   => $user_id,
										'Ateneo.id'        => $ateneo_id,
						), 'recursive' => 1

						));


		$this->set('Ateneo',$Ateneo);
    }


    function guardar(){

		$client_id = $this->Session->read('ClientesUsuarioLogueado.id'); // $_SESSION['Client']['id'];
		$user_id   = $this->Auth->user('id'); //$_SESSION['Auth']['User']['id'];

		if ($_POST){
			$data = $_POST['data'];
			$user    = $this->User->read(null,$this->Auth->user('id'));
//			pr($user);

			$url_api = 'http://www.comunidadaps.org.ar/api/creartema.php';

			$fecha   = explode('-', $user['User']['fecha_nacimiento'] );

			$send['NombreUsuario']          = $user['User']['name'];
			$send['ApellidoUsuario']        = $user['User']['lastname'];
			$send['EmailUsuario']           = $user['User']['email'];
			$send['FechaNacimientoUsuario'] = $fecha[2].'/'.$fecha[1].'/'.$fecha[0];
			$send['PaisUsuario']            = $user['Country']['name'];
			$send['SexoUsuario']            = 'Masculino'; /*---- ERROR NO esta definido en la tabla users */
			$send['TipoDocumentoUsuario']   = $user['Typodocument']['name'];
			$send['NroDocumentoUsuario']    = $user['User']['nro_documento'];
			$send['TituloTema']             = $data['Ateneo']['titulo'];
			$send['ContenidoTema']          = $data['Ateneo']['consulta'];

			$envio = "data=".json_encode($send);

			$handler = curl_init ();
			curl_setopt ( $handler, CURLOPT_URL, $url_api );
			curl_setopt ( $handler, CURLOPT_POST, true );
			curl_setopt ( $handler, CURLOPT_POSTFIELDS, $envio );
			curl_setopt ( $handler, CURLOPT_RETURNTRANSFER, true );
			$response = curl_exec ( $handler );
			$result = array ('response' => $response, 'data' => curl_getinfo ( $handler ) );
			curl_close ( $handler );


			if($result['data']['http_code']=='200'){
				$id_remoto = $result['response'];
			}

			
			$data['Ateneo']['client_id'] = $this->Session->read('ClientesUsuarioLogueado.id'); // $_SESSION['Client']['id'];
			$data['Ateneo']['user_id']   = $this->Auth->user('id'); //$_SESSION['Auth']['User']['id'];
			$data['Ateneo']['id']        = 0;
			$data['Ateneo']['id_remoto'] = $id_remoto;
			$data['Ateneo']['person_id'] = $data['Ateneo']['person_id'];;			

			$this->Ateneo->create();
			$this->Ateneo->save($data);
		}


		$this->redirect('/ateneos/');
		die();

    }

    function guardaropinion(){

		$client_id = $this->Session->read('ClientesUsuarioLogueado.id'); // $_SESSION['Client']['id'];
		$user_id   = $this->Auth->user('id'); //$_SESSION['Auth']['User']['id'];

		if ($_POST){
			$data = $_POST['data'];
			$user    = $this->User->read(null,$this->Auth->user('id'));

			$url_api = 'http://www.comunidadaps.org.ar/api/crearopinion.php';

			$fecha   = explode('-', $user['User']['fecha_nacimiento'] );

			$send['NombreUsuario']          = $user['User']['name'];
			$send['ApellidoUsuario']        = $user['User']['lastname'];
			$send['EmailUsuario']           = $user['User']['email'];
			$send['FechaNacimientoUsuario'] = $fecha[2].'/'.$fecha[1].'/'.$fecha[0];
			$send['PaisUsuario']            = $user['Country']['name'];
			$send['SexoUsuario']            = 'Masculino'; /*---- ERROR NO esta definido en la tabla users */
			$send['TipoDocumentoUsuario']   = $user['Typodocument']['name'];
			$send['NroDocumentoUsuario']    = $user['User']['nro_documento'];
			$send['IdProducto']   = $data['Ateneo']['id_remoto'];
			$send['TextoOpinion'] = $data['Ateneo']['comentario'];

			$envio = "data=".json_encode($send);

			$handler = curl_init ();
			curl_setopt ( $handler, CURLOPT_URL, $url_api );
			curl_setopt ( $handler, CURLOPT_POST, true );
			curl_setopt ( $handler, CURLOPT_POSTFIELDS, $envio );
			curl_setopt ( $handler, CURLOPT_RETURNTRANSFER, true );
			$response = curl_exec ( $handler );
			$result = array ('response' => $response, 'data' => curl_getinfo ( $handler ) );
			curl_close ( $handler );


			if($result['data']['http_code']=='200'){
				$respuesta = $result['response'];
			}
		}

		$this->redirect('/ateneos/');
		die();

    }    
//http://www.comunidadaps.org.ar/api/obtenercomentarios.php?idt=3305
	
}
?>